<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class T_bahan_pustaka extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();

		$this->load->library( 'template' ); 
		$this->load->model( 'model_t_bahan_pustaka' ); 
		
		$this->load->helper( 'form' );
		$this->load->helper( 'language' ); 
		$this->load->helper( 'url' );
        $this->load->model( 'model_auth' );

        $this->logged_in = $this->model_auth->check( TRUE );
        $this->template->assign( 'logged_in', $this->logged_in );

		$this->lang->load( 'db_fields', 'english' ); // This is the language file
	}



    /**
     *  LISTS MODEL DATA INTO A TABLE
     */         
    function index( $page = 0 )
    {
        $this->model_t_bahan_pustaka->pagination( TRUE );
		$data_info = $this->model_t_bahan_pustaka->lister( $page );
        $fields = $this->model_t_bahan_pustaka->fields( TRUE );
        

        $this->template->assign( 'pager', $this->model_t_bahan_pustaka->pager );
		$this->template->assign( 't_bahan_pustaka_fields', $fields );
		$this->template->assign( 't_bahan_pustaka_data', $data_info );
        $this->template->assign( 'table_name', 'T_bahan_pustaka' );
        $this->template->assign( 'template', 'list_t_bahan_pustaka' );
        
		$this->template->display( 'frame_admin.tpl' );
    }



    /**
     *  SHOWS A RECORD VIEW
     */
    function show( $id )
    {
		$data = $this->model_t_bahan_pustaka->get( $id );
        $fields = $this->model_t_bahan_pustaka->fields( TRUE );
        

        
        $this->template->assign( 'id', $id );
		$this->template->assign( 't_bahan_pustaka_fields', $fields );
		$this->template->assign( 't_bahan_pustaka_data', $data );
		$this->template->assign( 'table_name', 'T_bahan_pustaka' );
		$this->template->assign( 'template', 'show_t_bahan_pustaka' );
		$this->template->display( 'frame_admin.tpl' );
    }



    /**
     *  SHOWS A FROM, AND HANDLES SAVING IT
     */         
    function create( $id = false )
    {
		$this->load->library('form_validation');
        
		switch ( $_SERVER ['REQUEST_METHOD'] )
        {
            case 'GET':
                $fields = $this->model_t_bahan_pustaka->fields();
                
                
                
                $this->template->assign( 'action_mode', 'create' );
        		$this->template->assign( 't_bahan_pustaka_fields', $fields );
                $this->template->assign( 'metadata', $this->model_t_bahan_pustaka->metadata() );
        		$this->template->assign( 'table_name', 'T_bahan_pustaka' );
        		$this->template->assign( 'template', 'form_t_bahan_pustaka' );
        		$this->template->display( 'frame_admin.tpl' );
            break;

            /**
             *  Insert data TO t_bahan_pustaka table
             */
            case 'POST':
                $fields = $this->model_t_bahan_pustaka->fields();

                /* we set the rules */
                /* don't forget to edit these */
				$this->form_validation->set_rules( 'ID_BP', lang('ID_BP'), 'required|max_length[20]|integer' );
				$this->form_validation->set_rules( 'NO_DDC', lang('NO_DDC'), 'required|max_length[20]' );
				$this->form_validation->set_rules( 'KD_JENIS_BP', lang('KD_JENIS_BP'), 'required|max_length[2]' );
				$this->form_validation->set_rules( 'KD_BAHASA_BP', lang('KD_BAHASA_BP'), 'required|max_length[3]' );
				$this->form_validation->set_rules( 'KD_BENTUK_MEDIA', lang('KD_BENTUK_MEDIA'), 'required|max_length[3]' );
				$this->form_validation->set_rules( 'ID_KALA_TERBIT', lang('ID_KALA_TERBIT'), 'required|max_length[6]|integer' );
				$this->form_validation->set_rules( 'KD_PENERBIT', lang('KD_PENERBIT'), 'required|max_length[5]' );
				$this->form_validation->set_rules( 'KD_TEMPAT_TERBIT', lang('KD_TEMPAT_TERBIT'), 'required|max_length[5]' );
				$this->form_validation->set_rules( 'TAHUN_TERBIT', lang('TAHUN_TERBIT'), '4' );
				$this->form_validation->set_rules( 'JUDUL', lang('JUDUL'), 'required|max_length[200]' );
				$this->form_validation->set_rules( 'SUB_JUDUL', lang('SUB_JUDUL'), '200' );
				$this->form_validation->set_rules( 'EDISI', lang('EDISI'), '30' );
				$this->form_validation->set_rules( 'JUDUL_SERI', lang('JUDUL_SERI'), '200' );
				$this->form_validation->set_rules( 'ISBN_ISSN', lang('ISBN_ISSN'), '20' );
				$this->form_validation->set_rules( 'NO_PANGGIL', lang('NO_PANGGIL'), '30' );
				$this->form_validation->set_rules( 'PANJANG_BP', lang('PANJANG_BP'), '5' );
				$this->form_validation->set_rules( 'LEBAR_BP', lang('LEBAR_BP'), '5' );
				$this->form_validation->set_rules( 'JML_HALAMAN', lang('JML_HALAMAN'), '20' );
				$this->form_validation->set_rules( 'CATATAN_UMUM', lang('CATATAN_UMUM'), '200' );
				$this->form_validation->set_rules( 'GAMBAR_SAMPUL', lang('GAMBAR_SAMPUL'), '200' );
				$this->form_validation->set_rules( 'CONTENT', lang('CONTENT'), '200' );
				$this->form_validation->set_rules( 'EKSTENSI_FILE', lang('EKSTENSI_FILE'), '3' );
				$this->form_validation->set_rules( 'OPAC', lang('OPAC'), '1' );
				$this->form_validation->set_rules( 'BTS_LAMA_PINJAM_RESERVE', lang('BTS_LAMA_PINJAM_RESERVE'), '6' );
				$this->form_validation->set_rules( 'STS_PEMINJAMAN', lang('STS_PEMINJAMAN'), '1' );
				$this->form_validation->set_rules( 'STS_RESERVASI', lang('STS_RESERVASI'), '1' );
				$this->form_validation->set_rules( 'DENDA_HARIAN', lang('DENDA_HARIAN'), '18' );
				$this->form_validation->set_rules( 'USERNAME', lang('USERNAME'), 'required|max_length[20]' );
				$this->form_validation->set_rules( 'TANGGAL_AKSES', lang('TANGGAL_AKSES'), 'required' );

				$data_post['ID_BP'] = $this->input->post( 'ID_BP' );
				$data_post['NO_DDC'] = $this->input->post( 'NO_DDC' );
				$data_post['KD_JENIS_BP'] = $this->input->post( 'KD_JENIS_BP' );
				$data_post['KD_BAHASA_BP'] = $this->input->post( 'KD_BAHASA_BP' );
				$data_post['KD_BENTUK_MEDIA'] = $this->input->post( 'KD_BENTUK_MEDIA' );
				$data_post['ID_KALA_TERBIT'] = $this->input->post( 'ID_KALA_TERBIT' );
				$data_post['KD_PENERBIT'] = $this->input->post( 'KD_PENERBIT' );
				$data_post['KD_TEMPAT_TERBIT'] = $this->input->post( 'KD_TEMPAT_TERBIT' );
				$data_post['TAHUN_TERBIT'] = $this->input->post( 'TAHUN_TERBIT' );
				$data_post['JUDUL'] = $this->input->post( 'JUDUL' );
				$data_post['SUB_JUDUL'] = $this->input->post( 'SUB_JUDUL' );
				$data_post['EDISI'] = $this->input->post( 'EDISI' );
				$data_post['JUDUL_SERI'] = $this->input->post( 'JUDUL_SERI' );
				$data_post['ISBN_ISSN'] = $this->input->post( 'ISBN_ISSN' );
				$data_post['NO_PANGGIL'] = $this->input->post( 'NO_PANGGIL' );
				$data_post['PANJANG_BP'] = $this->input->post( 'PANJANG_BP' );
				$data_post['LEBAR_BP'] = $this->input->post( 'LEBAR_BP' );
				$data_post['JML_HALAMAN'] = $this->input->post( 'JML_HALAMAN' );
				$data_post['CATATAN_UMUM'] = $this->input->post( 'CATATAN_UMUM' );
				$data_post['GAMBAR_SAMPUL'] = $this->input->post( 'GAMBAR_SAMPUL' );
				$data_post['CONTENT'] = $this->input->post( 'CONTENT' );
				$data_post['EKSTENSI_FILE'] = $this->input->post( 'EKSTENSI_FILE' );
				$data_post['OPAC'] = $this->input->post( 'OPAC' );
				$data_post['BTS_LAMA_PINJAM_RESERVE'] = $this->input->post( 'BTS_LAMA_PINJAM_RESERVE' );
				$data_post['STS_PEMINJAMAN'] = $this->input->post( 'STS_PEMINJAMAN' );
				$data_post['STS_RESERVASI'] = $this->input->post( 'STS_RESERVASI' );
				$data_post['DENDA_HARIAN'] = $this->input->post( 'DENDA_HARIAN' );
				$data_post['USERNAME'] = $this->input->post( 'USERNAME' );
				$data_post['TANGGAL_AKSES'] = $this->input->post( 'TANGGAL_AKSES' );

                if ( $this->form_validation->run() == FALSE )
                {
                    $errors = validation_errors();
                    
                    
                    
                    
              		$this->template->assign( 'errors', $errors );
              		$this->template->assign( 'action_mode', 'create' );
            		$this->template->assign( 't_bahan_pustaka_data', $data_post );
            		$this->template->assign( 't_bahan_pustaka_fields', $fields );
                    $this->template->assign( 'metadata', $this->model_t_bahan_pustaka->metadata() );
            		$this->template->assign( 'table_name', 'T_bahan_pustaka' );
            		$this->template->assign( 'template', 'form_t_bahan_pustaka' );
            		$this->template->display( 'frame_admin.tpl' );
                }
                elseif ( $this->form_validation->run() == TRUE )
                {
                    $insert_id = $this->model_t_bahan_pustaka->insert( $data_post );
                    
					redirect( 't_bahan_pustaka' );
                }
            break;
        }
    }



    /**
     *  DISPLAYS THE POPULATED FORM OF THE RECORD
     *  This method uses the same template as the create method
     */
    function edit( $id = false )
    {
        $this->load->library('form_validation');

        switch ( $_SERVER ['REQUEST_METHOD'] )
        {
            case 'GET':
                $this->model_t_bahan_pustaka->raw_data = TRUE;
        		$data = $this->model_t_bahan_pustaka->get( $id );
                $fields = $this->model_t_bahan_pustaka->fields();
                
                
                
                
          		$this->template->assign( 'action_mode', 'edit' );
        		$this->template->assign( 't_bahan_pustaka_data', $data );
        		$this->template->assign( 't_bahan_pustaka_fields', $fields );
                $this->template->assign( 'metadata', $this->model_t_bahan_pustaka->metadata() );
        		$this->template->assign( 'table_name', 'T_bahan_pustaka' );
        		$this->template->assign( 'template', 'form_t_bahan_pustaka' );
        		$this->template->assign( 'record_id', $id );
        		$this->template->display( 'frame_admin.tpl' );
            break;
    
            case 'POST':
    
                $fields = $this->model_t_bahan_pustaka->fields();
                /* we set the rules */
                /* don't forget to edit these */
				$this->form_validation->set_rules( 'ID_BP', lang('ID_BP'), 'required|max_length[20]|integer' );
				$this->form_validation->set_rules( 'NO_DDC', lang('NO_DDC'), 'required|max_length[20]' );
				$this->form_validation->set_rules( 'KD_JENIS_BP', lang('KD_JENIS_BP'), 'required|max_length[2]' );
				$this->form_validation->set_rules( 'KD_BAHASA_BP', lang('KD_BAHASA_BP'), 'required|max_length[3]' );
				$this->form_validation->set_rules( 'KD_BENTUK_MEDIA', lang('KD_BENTUK_MEDIA'), 'required|max_length[3]' );
				$this->form_validation->set_rules( 'ID_KALA_TERBIT', lang('ID_KALA_TERBIT'), 'required|max_length[6]|integer' );
				$this->form_validation->set_rules( 'KD_PENERBIT', lang('KD_PENERBIT'), 'required|max_length[5]' );
				$this->form_validation->set_rules( 'KD_TEMPAT_TERBIT', lang('KD_TEMPAT_TERBIT'), 'required|max_length[5]' );
				$this->form_validation->set_rules( 'TAHUN_TERBIT', lang('TAHUN_TERBIT'), '4' );
				$this->form_validation->set_rules( 'JUDUL', lang('JUDUL'), 'required|max_length[200]' );
				$this->form_validation->set_rules( 'SUB_JUDUL', lang('SUB_JUDUL'), '200' );
				$this->form_validation->set_rules( 'EDISI', lang('EDISI'), '30' );
				$this->form_validation->set_rules( 'JUDUL_SERI', lang('JUDUL_SERI'), '200' );
				$this->form_validation->set_rules( 'ISBN_ISSN', lang('ISBN_ISSN'), '20' );
				$this->form_validation->set_rules( 'NO_PANGGIL', lang('NO_PANGGIL'), '30' );
				$this->form_validation->set_rules( 'PANJANG_BP', lang('PANJANG_BP'), '5' );
				$this->form_validation->set_rules( 'LEBAR_BP', lang('LEBAR_BP'), '5' );
				$this->form_validation->set_rules( 'JML_HALAMAN', lang('JML_HALAMAN'), '20' );
				$this->form_validation->set_rules( 'CATATAN_UMUM', lang('CATATAN_UMUM'), '200' );
				$this->form_validation->set_rules( 'GAMBAR_SAMPUL', lang('GAMBAR_SAMPUL'), '200' );
				$this->form_validation->set_rules( 'CONTENT', lang('CONTENT'), '200' );
				$this->form_validation->set_rules( 'EKSTENSI_FILE', lang('EKSTENSI_FILE'), '3' );
				$this->form_validation->set_rules( 'OPAC', lang('OPAC'), '1' );
				$this->form_validation->set_rules( 'BTS_LAMA_PINJAM_RESERVE', lang('BTS_LAMA_PINJAM_RESERVE'), '6' );
				$this->form_validation->set_rules( 'STS_PEMINJAMAN', lang('STS_PEMINJAMAN'), '1' );
				$this->form_validation->set_rules( 'STS_RESERVASI', lang('STS_RESERVASI'), '1' );
				$this->form_validation->set_rules( 'DENDA_HARIAN', lang('DENDA_HARIAN'), '18' );
				$this->form_validation->set_rules( 'USERNAME', lang('USERNAME'), 'required|max_length[20]' );
				$this->form_validation->set_rules( 'TANGGAL_AKSES', lang('TANGGAL_AKSES'), 'required' );

				$data_post['ID_BP'] = $this->input->post( 'ID_BP' );
				$data_post['NO_DDC'] = $this->input->post( 'NO_DDC' );
				$data_post['KD_JENIS_BP'] = $this->input->post( 'KD_JENIS_BP' );
				$data_post['KD_BAHASA_BP'] = $this->input->post( 'KD_BAHASA_BP' );
				$data_post['KD_BENTUK_MEDIA'] = $this->input->post( 'KD_BENTUK_MEDIA' );
				$data_post['ID_KALA_TERBIT'] = $this->input->post( 'ID_KALA_TERBIT' );
				$data_post['KD_PENERBIT'] = $this->input->post( 'KD_PENERBIT' );
				$data_post['KD_TEMPAT_TERBIT'] = $this->input->post( 'KD_TEMPAT_TERBIT' );
				$data_post['TAHUN_TERBIT'] = $this->input->post( 'TAHUN_TERBIT' );
				$data_post['JUDUL'] = $this->input->post( 'JUDUL' );
				$data_post['SUB_JUDUL'] = $this->input->post( 'SUB_JUDUL' );
				$data_post['EDISI'] = $this->input->post( 'EDISI' );
				$data_post['JUDUL_SERI'] = $this->input->post( 'JUDUL_SERI' );
				$data_post['ISBN_ISSN'] = $this->input->post( 'ISBN_ISSN' );
				$data_post['NO_PANGGIL'] = $this->input->post( 'NO_PANGGIL' );
				$data_post['PANJANG_BP'] = $this->input->post( 'PANJANG_BP' );
				$data_post['LEBAR_BP'] = $this->input->post( 'LEBAR_BP' );
				$data_post['JML_HALAMAN'] = $this->input->post( 'JML_HALAMAN' );
				$data_post['CATATAN_UMUM'] = $this->input->post( 'CATATAN_UMUM' );
				$data_post['GAMBAR_SAMPUL'] = $this->input->post( 'GAMBAR_SAMPUL' );
				$data_post['CONTENT'] = $this->input->post( 'CONTENT' );
				$data_post['EKSTENSI_FILE'] = $this->input->post( 'EKSTENSI_FILE' );
				$data_post['OPAC'] = $this->input->post( 'OPAC' );
				$data_post['BTS_LAMA_PINJAM_RESERVE'] = $this->input->post( 'BTS_LAMA_PINJAM_RESERVE' );
				$data_post['STS_PEMINJAMAN'] = $this->input->post( 'STS_PEMINJAMAN' );
				$data_post['STS_RESERVASI'] = $this->input->post( 'STS_RESERVASI' );
				$data_post['DENDA_HARIAN'] = $this->input->post( 'DENDA_HARIAN' );
				$data_post['USERNAME'] = $this->input->post( 'USERNAME' );
				$data_post['TANGGAL_AKSES'] = $this->input->post( 'TANGGAL_AKSES' );

                if ( $this->form_validation->run() == FALSE )
                {
                    $errors = validation_errors();
                    
                    
                    
                    
              		$this->template->assign( 'action_mode', 'edit' );
              		$this->template->assign( 'errors', $errors );
            		$this->template->assign( 't_bahan_pustaka_data', $data_post );
            		$this->template->assign( 't_bahan_pustaka_fields', $fields );
                    $this->template->assign( 'metadata', $this->model_t_bahan_pustaka->metadata() );
            		$this->template->assign( 'table_name', 'T_bahan_pustaka' );
            		$this->template->assign( 'template', 'form_t_bahan_pustaka' );
        		    $this->template->assign( 'record_id', $id );
            		$this->template->display( 'frame_admin.tpl' );
                }
                elseif ( $this->form_validation->run() == TRUE )
                {
				    $this->model_t_bahan_pustaka->update( $id, $data_post );
				    
					redirect( 't_bahan_pustaka/show/' . $id );   
                }
            break;
        }
    }



    /**
     *  DELETE RECORD(S)
     *  The 'delete' method of the model accepts int and array  
     */
    function delete( $id = FALSE )
    {
        switch ( $_SERVER ['REQUEST_METHOD'] )
        {
            case 'GET':
                $this->model_t_bahan_pustaka->delete( $id );
                redirect( $_SERVER['HTTP_REFERER'] );
            break;

            case 'POST':
                $this->model_t_bahan_pustaka->delete( $this->input->post('delete_ids') );
                redirect( $_SERVER['HTTP_REFERER'] );
            break;
        }
    }
}
